home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / IBSCAN.C < prev    next >
C/C++ Source or Header  |  1991-12-29  |  11KB  |  381 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* ibscan.c */
  21. /* Binary token scanner */
  22. #include "math_.h"
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "alloc.h"
  26. #include "dict.h"
  27. #include "iutil.h"
  28. #include "name.h"
  29. #include "ostack.h"
  30. #include "save.h"            /* for alloc_refs */
  31. #include "store.h"
  32. #include "stream.h"
  33. #include "btoken.h"
  34. #include "bseq.h"
  35. #include "bnum.h"
  36.  
  37. /* Import the system and user name tables */
  38. extern ref system_names, user_names;
  39.  
  40. /* Imported procedures */
  41. extern int array_get(P3(ref *, long, ref *));
  42.  
  43. /* Forward references */
  44. private    int    scan_binary_sequence(P2(stream *, ref *));
  45.  
  46. /* Scan a binary token.  Called from the main scanner */
  47. /* when it encounters an ASCII code 128-159, */
  48. /* if binary tokens are being recognized (object format != 0). */
  49. int
  50. scan_binary_token(register stream *s, ref *pref, int tcode)
  51. {    int num_format, code;
  52.     uint arg;
  53.     long nidx;
  54.     if ( seofp(s) ) return e_syntaxerror;
  55.     switch ( tcode )
  56.        {
  57.     case bt_seq_IEEE_msb:
  58.         s->num_format = num_msb + num_float_IEEE; goto bseq;
  59.     case bt_seq_IEEE_lsb:
  60.         s->num_format = num_lsb + num_float_IEEE; goto bseq;
  61.     case bt_seq_native_msb:
  62.         s->num_format = num_msb + num_float_native; goto bseq;
  63.     case bt_seq_native_lsb:
  64.         s->num_format = num_lsb + num_float_native;
  65. bseq:        return scan_binary_sequence(s, pref);
  66.     case bt_int32_msb:
  67.         num_format = num_msb + num_int32; goto num;
  68.     case bt_int32_lsb:
  69.         num_format = num_lsb + num_int32; goto num;
  70.     case bt_int16_msb:
  71.         num_format = num_msb + num_int16; goto num;
  72.     case bt_int16_lsb:
  73.         num_format = num_lsb + num_int16; goto num;
  74.     case bt_int8:
  75.         make_int(pref, (sgetc(s) ^ 128) - 128);
  76.         return 0;
  77.     case bt_fixed:
  78.         num_format = sgetc(s);
  79.         if ( !num_is_valid(num_format) )
  80.             return e_syntaxerror;
  81.         goto num;
  82.     case bt_float_IEEE_msb:
  83.         num_format = num_msb + num_float_IEEE; goto num;
  84.     case bt_float_IEEE_lsb:
  85.         num_format = num_lsb + num_float_IEEE; goto num;
  86.     case bt_float_native:
  87.         num_format = num_float_native;
  88. num:        s->num_format = num_format;
  89.         code = sget_encoded_number(s, pref);
  90.         switch ( code )
  91.            {
  92.         case t_integer: case t_real:
  93.             r_set_type(pref, code);
  94.             break;
  95.         case t_null: return e_syntaxerror;
  96.         default: return code;
  97.            }
  98.         return 0;
  99.     case bt_boolean:
  100.         arg = sgetc(s);
  101.         if ( arg & ~1 ) return e_syntaxerror;
  102.         make_bool(pref, arg);
  103.         return 0;
  104.     case bt_string_256:
  105.         arg = sgetc(s); goto str;
  106.     case bt_string_64k_msb:
  107.         arg = sgetc(s) << 8;
  108.         if ( seofp(s) ) return e_syntaxerror;
  109.         arg += sgetc(s);
  110.         goto str;
  111.     case bt_string_64k_lsb:
  112.         arg = sgetc(s);
  113.         if ( seofp(s) ) return e_syntaxerror;
  114.         arg += sgetc(s) << 8;
  115. str:       {    byte *str = (byte *)alloc(arg, 1, "string token");
  116.         uint rcnt;
  117.         if ( str == 0 ) return e_VMerror;
  118.         rcnt = sgets(s, str, arg);
  119.         if ( rcnt != arg ) return e_syntaxerror;
  120.         make_tasv(pref, t_string, a_all, arg, bytes, str);
  121.        }    return 0;
  122.     case bt_litname_system:
  123.         nidx = sgetc(s);
  124.         return array_get(&system_names, nidx, pref);
  125.     case bt_execname_system:
  126.         nidx = sgetc(s);
  127.         code = array_get(&system_names, nidx, pref);
  128.         if ( code < 0 ) return code;
  129.         r_set_attrs(pref, a_executable);
  130.         return 0;
  131.     case bt_litname_user:
  132.         nidx = sgetc(s);
  133.         code = array_get(&user_names, nidx, pref);
  134.         if ( code < 0 ) return code;
  135.         if ( !r_has_type(pref, t_name) ) return e_undefined;
  136.         return 0;
  137.     case bt_execname_user:
  138.         nidx = sgetc(s);
  139.         code = array_get(&user_names, nidx, pref);
  140.         if ( code < 0 ) return code;
  141.         if ( !r_has_type(pref, t_name) ) return e_undefined;
  142.         r_set_attrs(pref, a_executable);
  143.         return 0;
  144.     case bt_num_array:
  145.        {    ref *nap;
  146.         uint i;
  147.         num_format = sgetc(s);
  148.         if ( !num_is_valid(num_format) ) return e_syntaxerror;
  149.         s->num_format = num_format;
  150.         code = sgetshort(s, (short *)&arg);
  151.         if ( code < 0 ) return code;
  152.         nap = alloc_refs(arg, "number array token");
  153.         if ( nap == 0 ) return e_VMerror;
  154.         for ( i = 0; i < arg; i++ )
  155.            {    ref *np = nap + i;
  156.             int code = sget_encoded_number(s, np);
  157.             switch ( code )
  158.                {
  159.             case t_integer: case t_real:
  160.                 r_set_type(np, code);
  161.                 break;
  162.             case t_null: return e_syntaxerror;
  163.             default: return code;
  164.                }
  165.            }
  166.         make_tasv(pref, t_array, a_all, arg, refs, nap);
  167.        }    return 0;
  168.        }
  169.     return e_syntaxerror;
  170. }
  171.  
  172. /* Scan a binary object sequence. */
  173. private int
  174. scan_binary_sequence(register stream *s, ref *pref)
  175. {    ushort top_size = sgetc(s);
  176.     ushort size;
  177.     uint max_array_index;
  178.     uint min_string_index;
  179.     uint index;
  180.     int code;
  181.     register os_ptr op = osp;
  182. #if arch_is_big_endian
  183. #  define must_swap_bytes s_is_lsb(s)
  184. #else
  185. #  define must_swap_bytes s_is_msb(s)
  186. #endif
  187.     if ( top_size == 0 )
  188.        {    /* Extended header (2-byte array size, 4-byte length) */
  189.         ulong lsize;
  190.         if ( (code = sgetshort(s, (short *)&top_size)) < 0 ||
  191.              (code = sgetlong(s, (long *)&lsize)) < 0
  192.            )
  193.             return code;
  194.         if ( (size = (ushort)lsize) != lsize )
  195.             return e_limitcheck;
  196.         if ( size < 8 + sizeof(bin_seq_obj) )
  197.             return e_syntaxerror;
  198.         size -= 8;
  199.        }
  200.     else
  201.        {    /* Normal header (1-byte array size, 2-byte length) */
  202.         if ( (code = sgetshort(s, (short *)&size)) < 0 )
  203.             return code;
  204.         if ( size < 4 + sizeof(bin_seq_obj) )
  205.             return e_syntaxerror;
  206.         size -= 4;
  207.        }
  208.     /* First pass: read objects, handle all but composite. */
  209.     max_array_index = top_size;
  210.     min_string_index = size;
  211.     for ( index = 0; index < max_array_index; index++ )
  212.        {    bin_seq_obj ob;
  213.         byte bt;
  214.         if ( sgets(s, (byte *)&ob, sizeof(bin_seq_obj)) !=
  215.             sizeof(bin_seq_obj)
  216.            )
  217.             return e_syntaxerror;
  218.         if ( ++op > ostop )
  219.             return e_limitcheck;
  220. #define swap_size()\
  221.   if ( must_swap_bytes )\
  222.     bt = ob.size.b[0], ob.size.b[0] = ob.size.b[1], ob.size.b[1] = bt
  223. #define swap_value()\
  224.   if ( must_swap_bytes )\
  225.     bt = ob.value.b[0], ob.value.b[0] = ob.value.b[3], ob.value.b[3] = bt,\
  226.     bt = ob.value.b[1], ob.value.b[1] = ob.value.b[2], ob.value.b[2] = bt
  227.         switch ( ob.tx & 0x7f )
  228.            {
  229.         case bs_null:
  230.             make_null(op); break;
  231.         case bs_integer:
  232.             swap_value();
  233.             make_int(op, ob.value.w);
  234.             break;
  235.         case bs_real:
  236.             if ( ob.size.w != 0 )    /* fixed-point number */
  237.                {    swap_size(); swap_value();
  238.                 ob.value.f = (float)ldexp((float)ob.value.w,
  239.                               -ob.size.w);
  240.                }
  241.             else if ( (s->num_format & ~(num_lsb | num_msb)) !=
  242.                   num_float_native
  243.                 )
  244.                {    swap_value();
  245. #if !arch_floats_are_IEEE
  246.                 /* Convert IEEE float to native float. */
  247.                    {    int expt = (ob.value.w >> 23) & 0xff;
  248.                     long mant = ob.value.w & 0x7fffff;
  249.                     if ( expt == 0 && mant == 0 )
  250.                         ob.value.f = 0;
  251.                     else
  252.                        {    mant += 0x800000;
  253.                         ob.value.f = (float)
  254.                           ldexp((float)mant,
  255.                             expt - 127 - 24);
  256.                        }
  257.                     if ( ob.value.w < 0 )
  258.                         ob.value.f = -ob.value.f;
  259.                    }
  260. #endif
  261.                }
  262.             make_real(op, ob.value.f);
  263.             break;
  264.         case bs_boolean:
  265.             swap_value();
  266.             make_bool(op, (ob.value.w == 0 ? 0 : 1));
  267.             break;
  268.         case bs_string:
  269.             swap_size(); swap_value();
  270.             if ( ob.value.w < max_array_index ||
  271.                 ob.value.w + ob.size.w > size
  272.                )
  273.                 return e_syntaxerror;
  274.             if ( ob.value.w < min_string_index )
  275.                 min_string_index = (uint)ob.value.w;
  276.             make_tasv(op, t_string, (ob.tx < 128 ? a_all :
  277.                   a_all + a_executable), ob.size.w,
  278.                   intval, ob.value.w);
  279.             break;
  280.         case bs_name:
  281.         case bs_eval_name:
  282.             swap_size(); swap_value();
  283.             switch ( ob.size.w )
  284.                {
  285.             case 0:
  286.             case 0xffff:
  287.                 break;
  288.             default:
  289.                 if ( ob.value.w < max_array_index ||
  290.                     ob.value.w + ob.size.w > size
  291.                    )
  292.                     return e_syntaxerror;
  293.                 if ( ob.value.w < min_string_index )
  294.                     min_string_index = (uint)ob.value.w;
  295.                }
  296.             make_tasv(op, t_name,
  297.                   ((ob.tx & 0x7f) == bs_eval_name ? 0 :
  298.                    ob.tx & 0x80 ? a_all + a_executable :
  299.                    a_all), ob.size.w, intval, ob.value.w);
  300.             break;
  301.         case bs_array:
  302.             swap_size(); swap_value();
  303.             if ( ob.value.w + ob.size.w > min_string_index ||
  304.                 ob.value.w & (sizeof(bin_seq_obj) - 1)
  305.                )
  306.                 return e_syntaxerror;
  307.             max_array_index =
  308.               max(max_array_index,
  309.                   ob.value.w / sizeof(bin_seq_obj) + ob.size.w);
  310.             make_tasv(op, t_array, (ob.tx < 128 ? a_all :
  311.                   a_all + a_executable), ob.size.w,
  312.                   intval, ob.value.w);
  313.             break;
  314.         case bs_mark:
  315.             make_mark(op); break;
  316.         default:
  317.             return e_syntaxerror;
  318.            }
  319.        }
  320.     /* Allocate objects and strings. */
  321.     min_string_index =
  322.       min(min_string_index, max_array_index * sizeof(bin_seq_obj));
  323.        {    ref *rbase = alloc_refs(max_array_index,
  324.                     "binary object sequence(objects)");
  325.         uint str_size = size - min_string_index;
  326.         byte *sbase = (byte *)alloc(str_size, 1,
  327.                         "binary object sequence(strings)");
  328.         os_ptr op_top = op;
  329.         if ( rbase == 0 || sbase == 0 )
  330.            {    /* SHOULD FREE THEM */
  331.             return e_VMerror;
  332.            }
  333.         if ( sgets(s, sbase, str_size) != str_size )
  334.             return e_syntaxerror;
  335.         /* Fix up composites. */
  336.         for ( op = osp; op != op_top; )
  337.           switch ( r_type(++op) )
  338.            {
  339.         case t_string:
  340.             op->value.bytes =
  341.                 sbase + (op->value.intval - min_string_index);
  342.             break;
  343.         case t_array:
  344.             op->value.refs =
  345.                 rbase + (op->value.intval / sizeof(bin_seq_obj));
  346.             break;
  347.         case t_name:
  348.            {    ref rname;
  349.             switch ( r_size(op) )
  350.                {
  351.             case 0:
  352.                 code = array_get(&user_names,
  353.                          op->value.intval, &rname);
  354.                 goto usn;
  355.             case 0xffff:
  356.                 code = array_get(&system_names,
  357.                          op->value.intval, &rname);
  358. usn:                if ( code >= 0 && !r_has_type(&rname, t_name) )
  359.                     return e_undefined;
  360.                 break;
  361.             default:    /* ordinary name */
  362.                 code = name_ref(sbase + (op->value.intval - min_string_index), r_size(op), &rname, 0);
  363.                }
  364.             if ( code < 0 ) return code;
  365.             if ( !r_has_attr(op, a_read) )    /* bs_eval_name */
  366.                {    ref *defp = dict_find_name(&rname);
  367.                 if ( defp == 0 ) return e_undefined;
  368.                 rname = *defp;
  369.                }
  370.             else if ( r_has_attr(op, a_executable) )
  371.                 r_set_attrs(&rname, a_executable);
  372.             ref_assign(op, &rname);
  373.            }
  374.             break;
  375.            }
  376.         refcpy_to_new(rbase, osp + 1, max_array_index);
  377.         make_tasv(pref, t_array, a_all, top_size, refs, rbase);
  378.        }
  379.     return 0;
  380. }
  381.